/**
 * 是否在数组内
 */
export const inArray = (search, array) => {
	for (var i in array) {
		if (array[i] == search) return true
	}
	return false
}

// 折扣倒计时
export const getTimestampDiff = (startTimestamp, endTimestamp) => {
	// 计算两个时间戳之间相差的天数
	var dayDiff = Math.abs(Math.floor((endTimestamp - startTimestamp) / (24 * 60 * 60)));

	// 将天数转换为毫秒时间戳
	var millisecondDiff = dayDiff * 24 * 60 * 60 * 1000;

	return millisecondDiff;
}
/**
 * 数组交集
 * @param {Array} 数组1
 * @param {Array} 数组2
 * @return {Array}
 */
export const arrayIntersect = (array1, array2) => {
	return array1.filter(val => array2.indexOf(val) > -1)
}

export const URLParams = (path) => {
	let obj = {};
	let arr = path.split('?');
	if (arr.length > 1) {
		let query = arr[1].split('&');
		for (let i = 0, len = query.length; i < len; i++) {
			let key = query[i].split('=')[0],
				value = query[i].split('=')[1];
			obj[key] = value;
		}
	}
	return obj;
}

//检测是不是微信浏览器
export const isWeixin = () => {
	return navigator.userAgent.indexOf("MicroMessenger") > 0;
}

	//年-月-日 时：分：秒（2022-6-6 12:12:12）转 毫秒
export const getHTime = (date)=>{
	let startDate=date.replace(new RegExp("-","gm"),"/");
	let startDateM = (new Date(startDate)).getTime(); 
	return startDateM;
	}
export const formatDuring = (millisecond)=>{
	var days = parseInt(millisecond / (1000 * 60 * 60 * 24));
	var hours = parseInt((millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
	var minutes = parseInt((millisecond % (1000 * 60 * 60)) / (1000 * 60));
	var seconds = (millisecond % (1000 * 60)) / 1000;
	return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 ";
}


export const  timestampToDate = (timestamp)=>{
    var date = new Date(timestamp * 1000);
    var year = date.getFullYear();
    var month = ('0' + (date.getMonth() + 1)).slice(-2);
    var day = ('0' + date.getDate()).slice(-2);
    var hours = ('0' + date.getHours()).slice(-2);
    var minutes = ('0' + date.getMinutes()).slice(-2);
    var seconds = ('0' + date.getSeconds()).slice(-2);
    
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}
